﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;
using CE.PersonalAccident;

public partial class carinsurance_ViewBoughtCarInsurance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            txtAgentId.Text = Session[General.UserId].ToString();
            DateTime dtNow = new DateTime();
            dtNow = DateTime.Now;
            for (int i = 0; i > -31; i--)
            {
                DateTime dtTemp = dtNow.AddDays(i);
                string strDate = dtTemp.Date.Year.ToString("0000") + dtTemp.Date.Month.ToString("00") + dtTemp.Date.Day.ToString("00");
                ddlSubmittedDate.Items.Add(new ListItem(dtTemp.ToString("dd/MMM/yyyy"), strDate));
            }
            ddlSubmittedDate.Text = DateTime.Now.ToString("yyyyMMdd");
            ddlMonth.Text = DateTime.Now.Month.ToString("00");
            ddlYear.Text = DateTime.Now.Year.ToString("0000");
            txtSubmittedDate.Text = ddlYear.Text + ddlMonth.Text;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        txtSearchKey.Text = "0";
        if (txtSearch.Text.Trim() != "")
        {
            txtSearchKey.Text = txtSearch.Text.Trim();
        }
        txtSubmittedDate.Text = ddlYear.Text + ddlMonth.Text;
        grvCarInsurance.DataBind();
    }

    private void ClearFields()
    {
        divCallStatus.Visible = false;
        divCallPA.Visible = false;
        ddlCallStatus.Text = "-1";
        lblCallStatusMessage.Text = "";
        txtCarId.Text = "0";
        txtCustomerId.Text = "0";
        txtRemark.Text = "";
    }
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtSubmittedDate.Text = ddlYear.Text + ddlMonth.Text;
        //btnSubmit_Click(sender, e);
        //divCallStatus.Visible = false;
        //lblCallStatusMessage.Text = "";
        //txtCarId.Text = "0";
        //txtCustomerId.Text = "0";
        //txtRemark.Text = "";
        ClearFields();
    }

    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtSubmittedDate.Text = ddlYear.Text + ddlMonth.Text;
        //btnSubmit_Click(sender, e);
        //divCallStatus.Visible = false;
        //divCallPA.Visible = false;
        //lblCallStatusMessage.Text = "";
        //txtCarId.Text = "0";
        //txtCustomerId.Text = "0";
        //txtRemark.Text = "";
        ClearFields();
    }

    protected void ddlSubmittedDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSubmit_Click(sender, e);
        //divCallStatus.Visible = false;
        //divCallPA.Visible = false;
        //lblCallStatusMessage.Text = "";
        //txtCarId.Text = "0";
        //txtCustomerId.Text = "0";
        //txtRemark.Text = "";
        ClearFields();
    }

    protected void ddlProviderCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        //btnSubmit_Click(sender, e);
        //divCallStatus.Visible = false;
        //divCallPA.Visible = false;
        //lblCallStatusMessage.Text = "";
        //txtCarId.Text = "0";
        //txtCustomerId.Text = "0";
        //txtRemark.Text = "";
        ClearFields();
    }

    protected void grvCarInsurance_PageIndexChanged(object sender, EventArgs e)
    {
        grvCarInsurance.SelectedIndex = -1;
    }

    protected void grvCarInsurance_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvCarInsurance.Rows.Count > 0 && grvCarInsurance.SelectedIndex >= 0)
        {
            ClearFields();
            DataKey myData = grvCarInsurance.SelectedDataKey;
            txtCarId.Text = myData[1].ToString();
            txtCustomerId.Text = myData[2].ToString();
            txtProviderCompanyId.Text = ddlProviderCompany.Text;
            txtContactNo.Text = myData[3].ToString();
            txtCIQuoteRefNo.Text = myData[4].ToString();

            divCallStatus.Visible = true;
            divCallPA.Visible = true;
            PACallStatus callstatus = new PACallStatus();
            PACallStatusDB callstatusdb = new PACallStatusDB();
            string paInsuranceURL = CE.Configuration.MyConfigurationManager.GetAppSettingValue("PAServerPath");
            hplPANewQuote.NavigateUrl = paInsuranceURL + "AgentLogin.aspx?uid=" + Session[General.UserId].ToString() +
                                "&cid=" + txtCustomerId.Text + "&pcid=" + ddlProviderCompany.Text;
            hplPANewUser.NavigateUrl = paInsuranceURL + "AgentLogin.aspx?uid=" + Session[General.UserId].ToString() +
                                "&pcid=" + ddlProviderCompany.Text;
            callstatus = callstatusdb.GetCallStatus(Convert.ToInt64(txtCarId.Text), Convert.ToInt64(txtCustomerId.Text));

            if (callstatus != null)
            {
                txtCallStatusId.Text = callstatus.CallStatusId.ToString();
                ddlCallStatus.Text = callstatus.AgentCallStatus.ToString();
                txtRemark.Text = Server.HtmlDecode(callstatus.Remark);
            }
            else
            {
                txtCallStatusId.Text = "0";
            }
            callstatus = null;
            callstatusdb = null;
        }
    }

    protected void btnCallStatus_Click(object sender, EventArgs e)
    {
        lblCallStatusMessage.Text = "";
        Page.Validate();
        if (Page.IsValid)
        {
            if (txtCarId.Text.Length > 0 && txtCustomerId.Text.Length > 0)
            {
                PACallStatus callstatus = new PACallStatus();
                PACallStatusDB callstatusdb = new PACallStatusDB();
                if (txtCallStatusId.Text.Length > 0 && txtCallStatusId.Text != "0")
                {
                    callstatus.CallStatusId = Convert.ToInt64(txtCallStatusId.Text);
                }
                else
                {
                    callstatus.CallStatusId = 0;
                }
                callstatus.CarId = Convert.ToInt64(txtCarId.Text);
                callstatus.CIProviderCompanyId = Convert.ToInt64(txtProviderCompanyId.Text);
                callstatus.CustomerId = Convert.ToInt64(txtCustomerId.Text);
                callstatus.ContactNo = txtContactNo.Text;
                callstatus.CIQuoteRefNo = txtCIQuoteRefNo.Text;
                callstatus.CallTypeId = 200;
                callstatus.AgentId = Convert.ToInt64(txtAgentId.Text);
                callstatus.AgentCallStatus = Convert.ToInt32(ddlCallStatus.Text);
                callstatus.Remark = Server.HtmlEncode(txtRemark.Text);
                callstatus.CallStatusId = callstatusdb.Insert(callstatus);
                if (callstatus.CallStatusId > 0)
                {
                    if (txtCallStatusId.Text.Length > 0 && txtCallStatusId.Text != "0")
                    {
                        lblCallStatusMessage.Text = "UPDATE";
                    }
                    else
                    {
                        txtCallStatusId.Text = callstatus.CallStatusId.ToString();
                        lblCallStatusMessage.Text = "SUCCESS";
                    }
                }
                else
                {
                    lblCallStatusMessage.Text = "FAIL";
                }
                callstatus = null;
                callstatusdb = null;
            }
            else
            {
                lblCallStatusMessage.Text = "FAIL";
            }
        }
    }

}
